<?php
/**
 * Model for a PHP Mess Detection log.
 *
 * @category PhpCilux
 * @package  PhpCilux
 */

/**
 * Model for a PHP Mess Detection log.
 *
 * @package PhpCilux
 */
class Model_Log_PhpMd extends Model_Log_Abstract
{

    /**
     * Constructor.
     *
     * @param string $projectKey The project key.
     * @param string $buildKey   The build key.
     */
    public function __construct($projectKey, $buildKey)
    {
        parent::__construct($projectKey, $buildKey);

        $this->_logFilename = 'phpmd.txt';
    }

    /**
     * Get the status of the PHP Codesniffer log.
     *
     * @return string
     */
    public function status()
    {
        $status = 'success';

        $log   = $this->getContent();
        $lines = explode("\n", $log);

        while (list(, $line) = each($lines)) {
            $line = trim($line);

            if ('' != $line) {
                return 'warning';
            }
        }

        return $status;
    }

    /**
     * Formating the log.
     *
     * @param string $content The log content which will be styled.
     *
     * @return string
     */
    public function format($content)
    {
        $lines   = explode("\n", $content);
        $content = '<h4>PHPMD - PHP Mess Detection</h4>';

        $warnings = 0;
        $last     = '';

        while (list(, $line) = each($lines)) {
            $line = trim($line);

            switch(true) {
                case '' == $line:
                    break;

                default:
                    $parts = explode("\t", $line);

                    $content .= '<pre>' . $parts[0] . '</pre>';
                    if (isset($parts[1])) {
                        $content .= '<div class="warning"><pre>' . $parts[1] . '</pre></div>';
                        $warnings++;
                    }

                    $last = '';
                    break;
            }
        }

        $content .= '<br />';
        $content .= $this->_formatSummary($warnings);
        $content .= '<br />';

        return $content;
    }

    /**
     * Create the summary line.
     *
     * @param integer $warnings The warnings count.
     *
     * @return $string;
     */
    private function _formatSummary($warnings = 0)
    {
        switch(true) {
            case ($warnings > 0):
                $class   = 'warning';
                $summary = 'Found ' . $warnings . ' warning(s)';
                break;

            default:
                $class   = 'success';
                $summary = 'OK';
                break;
        }

        $content .= "\n" . '<div class="' . $class . '"><pre>';
        $content .= $summary;
        $content .= "</pre></div>\n";

        return $content;
    }

}